import 'package:shared_preferences/shared_preferences.dart';

import '../model/user_model.dart';

class User {

  static final User _singleton = User._internal();

  factory User() {
    return _singleton;
  }

  User._internal();

  List<String>? cookie;
  String? userName;

  void saveUserInfo(UserModel userModel, List<String>? cookies) {
    cookie = cookies;
    userName = userModel.username;
    saveInfo();
  }

  Future<Null> getUserInfo() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    List<String>? cookies = sp.getStringList("cookies");
    if (cookies != null) {
      cookie = cookies;
    }

    String? username = sp.getString("username");
    if(username!=null) {
      userName = username;
    }
  }

  saveInfo() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    sp.setStringList("cookies", cookie!);
    sp.setString("username", userName!);
  }

  void clearUserInfo() {
    cookie = null;
    userName = null;
    clearInfo();
  }

  clearInfo() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    sp.remove("cookies");
    sp.remove("username");
  }
}
